<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TCP粘包与拆包现象深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
            animation: drift 20s infinite linear;
        }
        
        @keyframes drift {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 80%;
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -2px;
            left: -2px;
            right: -2px;
            bottom: -2px;
            background: linear-gradient(45deg, #f093fb, #f5576c, #4facfe, #00f2fe);
            border-radius: 12px;
            opacity: 0.3;
            z-index: -1;
            animation: gradient-border 3s ease infinite;
        }
        
        @keyframes gradient-border {
            0%, 100% { opacity: 0.3; }
            50% { opacity: 0.6; }
        }
        
        .concept-card {
            background: white;
            border-radius: 16px;
            padding: 24px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .concept-card:hover {
            border-color: #667eea;
            transform: translateY(-8px);
            box-shadow: 0 15px 40px rgba(102, 126, 234, 0.2);
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .solution-item {
            background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
            padding: 20px;
            border-radius: 12px;
            margin-bottom: 16px;
            position: relative;
            overflow: hidden;
        }
        
        .solution-item::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 4px;
            height: 100%;
            background: #ff6b6b;
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0 8px 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                    TCP粘包与拆包现象
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    深入理解TCP协议中的数据传输机制与边界问题
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-full px-6 py-3">
                        <i class="fas fa-network-wired mr-2"></i>
                        网络协议
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-full px-6 py-3">
                        <i class="fas fa-code mr-2"></i>
                        编程实践
                    </div>
                    <div class="bg-white bg-opacity-20 backdrop-filter backdrop-blur-lg rounded-full px-6 py-3">
                        <i class="fas fa-shield-alt mr-2"></i>
                        可靠传输
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="max-w-4xl mx-auto mb-16">
            <div class="content-card rounded-2xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">T</span>CP粘包和拆包现象是TCP协议在传输数据时常见的问题。它们发生在数据的发送和接收过程中，主要是由于TCP协议的流式特性和数据传输的机制所导致的。理解这些现象对于开发高质量的网络应用程序至关重要。
                </p>
            </div>
        </div>

        <!-- Definitions -->
        <div class="max-w-6xl mx-auto mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">核心概念定义</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="concept-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-compress"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">粘包（Packet Stickiness）</h3>
                    <p class="text-gray-600 leading-relaxed">
                        粘包是指发送端发送的多个数据包在接收端被合并成一个数据包的现象。即接收端一次性从TCP接收缓冲区读取到多个发送的数据包，导致这些数据包"粘"在一起。
                    </p>
                </div>
                <div class="concept-card">
                    <div class="icon-wrapper">
                        <i class="fas fa-expand"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4 text-gray-800">拆包（Packet Splitting）</h3>
                    <p class="text-gray-600 leading-relaxed">
                        拆包是指发送端发送的一个较大的数据包在接收端被分成了多个小的数据包接收的现象。即接收端从TCP接收缓冲区多次读取才能获得完整的发送数据。
                    </p>
                </div>
            </div>
        </div>

        <!-- Visualization -->
        <div class="max-w-6xl mx-auto mb-16">
            <h2 class="text-3xl font-bold mb-8 section-title">数据传输流程可视化</h2>
            <div class="mermaid">
                graph LR
                    A[应用层数据] -->|write/send| B[TCP发送缓冲区]
                    B -->|TCP分段| C[网络传输]
                    C -->|接收| D[TCP接收缓冲区]
                    D -->|read/recv| E[应用层接收]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f5576c,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#4facfe,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Causes -->
        <div class="max-w-6xl mx-auto mb-16">
            <div class="highlight-box mb-8">
                <h2 class="text-3xl font-bold mb-4">
                    <i class="fas fa-question-circle mr-3"></i>
                    粘包和拆包现象产生的原因
                </h2>
                <p class="text-lg opacity-90">
                    TCP协议是面向连接的、可靠的、基于字节流的协议。TCP并不保留应用程序的边界，发送端发送的多次写操作产生的数据可能会被TCP协议粘合成一个包，也可能将一次写操作的数据分成多个包发送。
                </p>
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <!-- 粘包原因 -->
                <div>
                    <h3 class="text-2xl font-bold mb-6 text-gray-800">
                        <i class="fas fa-compress-arrows-alt mr-2 text-purple-600"></i>
                        粘包的原因
                    </h3>
                    <div class="space-y-4">
                        <div class="bg-purple-50 p-6 rounded-lg border-l-4 border-purple-500">
                            <h4 class="font-bold text-lg mb-2">1. 发送端的TCP缓冲区机制</h4>
                            <p class="text-gray-700">
                                发送端的应用程序可能在短时间内调用了多次<code class="bg-gray-200 px-2 py-1 rounded">send</code>或<code class="bg-gray-200 px-2 py-1 rounded">write</code>操作，这些操作的数据可能被存储在发送缓冲区中。当TCP准备将数据发送到网络时，可能会一次性将缓冲区中的多个数据包合并成一个TCP段发送出去。
                            </p>
                        </div>
                        <div class="bg-purple-50 p-6 rounded-lg border-l-4 border-purple-500">
                            <h4 class="font-bold text-lg mb-2">2. 接收端的TCP缓冲区机制</h4>
                            <p class="text-gray-700">
                                接收端的应用程序从TCP接收缓冲区中读取数据时，可能会一次性读取多个逻辑数据包的数据。如果应用程序没有设置合理的读取逻辑，一次<code class="bg-gray-200 px-2 py-1 rounded">recv</code>或<code class="bg-gray-200 px-2 py-1 rounded">read</code>操作可能会读取到多个粘在一起的包。
                            </p>
                        </div>
                        <div class="bg-purple-50 p